$(function(){

    $(".shouhuo_address .address_right").hover(function(){
        $(this).css("backgroundColor","#fff3f3");
        $(this).find(".default_address").show();
        $(this).find(".bianji").show();
        $(this).find(".dele").show();
    },function(){
        $(this).css("backgroundColor","#fff");
        $(this).find(".default_address").hide();
        $(this).find(".bianji").hide();
        $(this).find(".dele").hide();
    })
    // 收货信息样式切换
    $(".address_left").click(function(){
        $(this).css("border","2px solid #e4393c");
        $(this).find("em").show();
        $(this).parent().siblings(".shouhuo_address").find(".address_left").css("border","2px solid #dddddd").find("em").hide();
        //获取收货信息的姓名,将获取到的姓名赋值给订单的末尾
            const name = $(".address_right span:first").text();
            $(".shouhuo_people span:first").text(name);
        //获取手机号    
            const phone = $(this).parent().find(".address_right .uPhone").text();
            //console.log(phone);
            //将获取到的手机号码赋值给订单末尾
            $(".shouhuo_people .uPhone").text(phone);
        //获取地址
            //获取区域
            const quyu = $(this).parent().find(".address_right .uAddress").text();
            //console.log(quyu);
            //将获取到的quyu,赋值给订单末尾
            $(".jisong_address .uAddress").text(quyu);
            //获取详细地址
            const detailed_address = $(this).parent().find(".address_right .u_gu_address").text();
            $(".jisong_address .u_gu_address").html(detailed_address);
        const name_jia = $(this).parent().find(".address_right span:first").text();
        const dizhi = $(this).parent().find("")
        //.siblings(".shouhuo_address") 这里是将位置对换
        $(".shouhuo_people span:first").html(name_jia);
        
    })
    // 支付方式样式切换
    $(".zifu_a").click(function(){
        $(this).css("border","2px solid #e4393c").siblings().css("border","2px solid #dddddd");
        $(this).find("i").show();
        $(this).siblings().find("i").hide();
    })

    // 设置为默认地址
    $(".shouhuo_address:first .address_right .moren_biaoshi").show();
    $(".default_address").click(function(){
        $(this).siblings(".moren_biaoshi").show();
        $(this).parents(".shouhuo_address").siblings().find(".address_right .moren_biaoshi").hide();
    })

    // 删除地址
    $(".dele").click(function(){
        if(confirm("您确认要删除该收货地址吗？")){
            $(this).parents(".shouhuo_address").remove();
        }
    })

    // 点击打开新增收货地址框
    $(".shouhuo_top a").click(function(){
        $(".reset").click();
        isAdd=true;
        $(".xuan_name .address_text").val("");
        $(".xuan_shou_address .address_text").val("");
        $(".xuan_phone .phone_text").val("");
        $(".xuan_gu_phone .gu_phone_text").val("");
        $(".xuan_email .email_text").val("");
        $(".xuan_address_bie_text").val("");
        $(".add_address_big").show();
    })

    // 点击关闭新增收货地址框
    $(".address_top_title .close_box").click(function(){
        $(".add_address_big").hide();
    })

    // 地址别名常用名称点击赋值
    $(".changyong_name").click(function(){
        $(".xuan_address_bie_text").val($(this).text());
    })

    var isAdd = true;

    // 保存收货人信息
    $(".baocun_info").click(function(){
            if(isAdd){
                //新增
                var newShou_address = $(".shouhuo_bottom .shouhuo_address:last").clone(true);
                if($(".xuan_address_bie_text").val()==""){
                    $(newShou_address).find(".address_left span").text($(".xuan_name .address_text").val());
                }else{
                    $(newShou_address).find(".address_left span").text($(".xuan_address_bie .xuan_address_bie_text").val());
                }
                $(newShou_address).find(".address_right .uName").text($(".xuan_name .address_text").val());
                $(newShou_address).find(".address_right .uAddress").text($("#bigOne #totalConfirm").val());
                $(newShou_address).find(".address_right .u_gu_address").text($(".xuan_shou_address .address_text").val());
                $(newShou_address).find(".address_right .uPhone").text($(".xuan_phone .phone_text").val());
                $(".shouhuo_bottom").append(newShou_address);
            }else{
                //修改
                if($(".xuan_address_bie_text").val()==""){
                    $(".shouhuo_bottom .shouhuo_address").eq(i).find(".address_left span").text($(".xuan_name .address_text").val());
                }else{
                    $(".shouhuo_bottom .shouhuo_address").eq(i).find(".address_left span").text($(".xuan_address_bie .xuan_address_bie_text").val());
                }
                $(".shouhuo_bottom .shouhuo_address").eq(i).find(".address_right .uName").text($(".xuan_name .address_text").val());   
                $(".shouhuo_bottom .shouhuo_address").eq(i).find(".address_right .uAddress").text($("#bigOne #totalConfirm").val());   
                $(".shouhuo_bottom .shouhuo_address").eq(i).find(".address_right .u_gu_address").text($(".xuan_shou_address .address_text").val());   
                $(".shouhuo_bottom .shouhuo_address").eq(i).find(".address_right .uPhone").text($(".xuan_phone .phone_text").val());   
            }
            $(".add_address_big").hide();
    })
    var i=0;
    // 点击编辑修改地址
    $(".bianji").click(function(){
        i = $(this).parents(".shouhuo_address").index();
        isAdd = false;
        $(".confirm").click();
        $(".add_address_big").show();
        $("#bigOne #totalConfirm").val($(this).siblings(".uAddress").text());
        $(".xuan_name .address_text").val($(this).siblings(".uName").text());
        $(".xuan_shou_address .address_text").val($(this).siblings(".u_gu_address").text());
        $(".xuan_phone .phone_text").val($(this).siblings(".uPhone").text());
        $(".xuan_gu_phone .gu_phone_text").val($(this).siblings(".uPhone").text());
        $(".xuan_email .email_text").val("517663950@qq.com");
        if($(this).parent().siblings(".address_left").find("span").text()=="家里" || $(this).parent().siblings(".address_left").find("span").text()=="父母家" || $(this).parent().siblings(".address_left").find("span").text()=="公司"){
            $(".xuan_address_bie_text").val($(this).parent().siblings(".address_left").find("span").text());
        }else{
            $(".xuan_address_bie_text").val($(this).siblings(".uName").text());
        }
    })
})